<?php
class HandleBox
{
	/**
	 * 获取分类
	 */
	public static  function getCategory()
	{
		$criteria = new CDbCriteria(array(
			'condition'=>'fid = :Fid',
			'params'=>array(':Fid'=>0),
			'group'=>'path',
		));
		$category = Category::model()->findAll($criteria);
		$catArray = array();
		$i = 0;
		foreach ($category as $value){
			$catArray[$i]['label'] = $value->name;
			$catArray[$i]['url'] = array('/category/view', 'id'=>$value->id);
			$subCats = Category::model()->findAllByAttributes(array('fid'=>$value->id));
			if ($subCats){
				foreach ($subCats as $cat)
					$catArray[$i]['items'][] = array('label'=>$cat->name, 'url'=>array('/category/view', 'id'=>$cat->id));
			}
			$i++;
		}
		return $catArray;
	} 
	
	/**
	 * Node表单提交的后续处理
	 * 生成node的历史版本
	 * @param object $nodeOld，未更新前的node
	 * @param array $dataArray，提交的表单数据
	 * @param bool $isNewNode，新建Node为true，更新Node为false，默认true
	 */
	public function nodeRevisionHandle($id, $bodyOld=null, $bodyNew)
	{
		$change = "";
		$countText = 0;
		
		if ($bodyOld === null){
				$countText = mb_strlen($this->deleteHtml($bodyNew), 'UTF8');
				$change = "创建，撰写了".$countText."个字。";
				$this->createNodeRevision($id, $bodyNew, $change);
				Yii::app()->user->setFlash('nodeHandleInfo',"内容创建成功。");
		}
		else{
			// 判断内容有没有更新
			if($bodyOld != $bodyNew){
				$countText = mb_strlen($this->deleteHtml($bodyNew), 'UTF8') - mb_strlen($this->deleteHtml($bodyOld), 'UTF8');
				// 创建事件的历史版本
				if ($countText > 0){
					$change = "创建，增加了".$countText."个字。";
				}
				if($countText < 0){
					$change = "创建，减少了".substr($countText, 1)."个字。";
				}
				$change = substr($change, 0);
				if ($this->createNodeRevision($id, $bodyNew, $change)){
					Yii::app()->user->setFlash('nodeHandleInfo',"您已经修改成功，生成了新的版本。");
				}
			}
			else{
				Yii::app()->user->setFlash('nodeHandleInfo',"您未做内容上的修改，没有为该次编辑生成新的版本。");
			}
		}
	}
	
	/**
	 * 处理链接，如果存在相关内容，则添加链接映射
	 * 如何不存在，则创建
	 * @param int $id
	 * @param array $dataArray
	 */
	public function nodeMappingHandle($mid, $dataArray)
	{
		$model = new NodeMapping;
		$node = new Node;
		$model->model()->deleteAllByAttributes(array('mid'=>$mid));
		foreach ($dataArray as $key=>$value){
			if ($value != null){
				foreach ($value as $title){
					$row = $node->findByAttributes(array('title'=>$title, 'tid'=>$key));
					if (!$row){
						unset($node->id);
						$node->setIsNewRecord(true);
						$node->cid = 0;
						$node->uid = 0;
						$node->fid = 0;
						$node->tid = $key;
						$node->title = $title;
						$node->body = null;
						$node->status = DefineBox::NODE_STATUS_EMPTY;
						$node->access = DefineBox::NODE_ACCESS_PUBLIC;
						$node->level = 0;
						$node->create_time = $node->update_time = 0;
						if($node->save(false)){
							$model->sid = $node->id;
						}
					}
					else{
						$model->sid = $row->id;
					}
					unset($model->id);
					$model->setIsNewRecord(true);
					$model->mid = $mid;
					$model->save(false);
				}
			}
		}
	}
	
	/**
	 * 生成Node历史版本
	 */
	protected function createNodeRevision($id, $bodyNew, $change)
	{
		$modelNodeRevision = new NodeRevision;
		$modelNodeRevision->nid = $id;
		$modelNodeRevision->uid = Yii::app()->user->id;
		$modelNodeRevision->body = $bodyNew;
		$modelNodeRevision->change = $change;
		$modelNodeRevision->update_time = time();
		return $modelNodeRevision->save();
	}
	
	/**
	 * tag和tagTotal的处理方法
	 */
	public function tagHandle($nid, $tid, $tagArray)
	{
		// 清理原tagTotal
		$tagOld = Tag::model()->findAllByAttributes(array('nid'=>$nid));
		if ($tagOld){
			foreach ($tagOld as $tag){
				$tagTotalOld = TagTotal::model()->findByPk($tag->tag);
				if ($tagTotalOld){
					if ($tagTotalOld->amount > 1){
						$tagTotalOld->amount -= 1;
						$tagTotalOld->update();
					}
					else{
						$tagTotalOld->delete();
					}
				}
			}
			// 擦除原tag
			Tag::model()->deleteAllByAttributes(array('nid'=>$nid));
		}
		// 处理新提交的tag
		$tagNew = new Tag;
		foreach ($tagArray as $tag){
			// 在tag表中新增
			$tagNew->setIsNewRecord(true);
			unset($tagNew->id);
			$tagNew->nid = $nid;
			$tagNew->tid = $tid;
			$tagNew->tag = $tag;
			$tagNew->save();
	
			// 在tagTotal中新增
			$tagTotalNew = TagTotal::model()->findByPk($tag);
			if ($tagTotalNew){
				$tagTotalNew->amount ++;
			}
			else{
				$tagTotalNew = new TagTotal;
				$tagTotalNew->tag = $tag;
				$tagTotalNew->amount = 1;
			}
			$tagTotalNew->save();
		}
	}
	
	/**
	 * 显示tags标签
	 * @param string $tags
	 */
	public static function  tagDisplay($tags)
	{
		$tagsNew = '';
		foreach ($tags as $value){
			$value = CHtml::link($value, array('/tag', 'tag'=>$value));
			$tagsNew .= $value." | ";
		}
		return substr($tagsNew, 0, -2);
	}
	
	public static function isUserHelper($id)
	{
		$isUserHelper = "";
		// 是否合作者
		$row = UserHelper::model()->findByAttributes(array(
				'uid'=>Yii::app()->user->id, 'hid'=>$id));
		if ($row){
			$iUsersHelper = true;
		}
		else{
			$isUserHelper = false;
		}
		return $isUserHelper;
	}
	
	/**
	 * 获取Page类型
	 * return strint $type
	 */
	public static function getPageType($tid)
	{
		switch($tid){
			case DefineBox::PAGE_TYPE_ABOUT:
				$type = '简介';
				break;
			case DefineBox::PAGE_TYPE_NEWS:
				$type = '新闻';
				break;
			case DefineBox::PAGE_TYPE_MANUAL:
				$type = '手册';
				break;
		}
		return $type;
	}
	
	/**
	 * 获取Node类型
	 * return strint $type
	 */
	public static function getNodeType($tid)
	{
		switch($tid){
			case DefineBox::EVENT_TYPE_CODE:
				$type = '事件';
				break;
			case DefineBox::SCENE_TYPE_CODE:
				$type = '地点';
				break;
			case DefineBox::ROLE_TYPE_CODE:
				$type = '角色';
				break;
			case DefineBox::OBJECT_TYPE_CODE:
				$type = '物品';
				break;
			case DefineBox::PEDIA_TYPE_CODE:
				$type = '知识';
				break;
		}
		return $type;
	}
	
	/**
	 * 处理社区面包屑
	 */
	public static function getTopicType(){
		switch ($_GET['tid']){
			case DefineBox::TOPIC_TYPE_NOTICE:
				$type = '社区公告';
				break;
			case DefineBox::TOPIC_TYPE_NODE:
				$type = '内容建设';
				break;
			case DefineBox::TOPIC_TYPE_WORK:
				$type = '站点事务';
				break;
		}
		return $type;
	}
	
	/**
	 * 获取投票类型
	 */
	public static function getPollType($tid)
	{
		switch ($tid){
			case DefineBox::POLL_LEVEL_CODE:
				$type = "成熟度";
				break;
		}
		return $type;
	}
	
	/**
	 * 过滤HTML代码，包括空格，回车符等。
	 */
	public static function deleteHtml($str)
	{
		$str = trim($str);
		$str=strip_tags($str,"");
		$str=preg_replace("{\t}","",$str);
		$str=preg_replace("{\r\n}","",$str);
		$str=preg_replace("{\r}","",$str);
		$str=preg_replace("{\n}","",$str);
		$str=preg_replace("{ }","",$str);
		return $str;
	}
}